#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Módulos 
import sys, gtk, pygame, utils, objetos, inputbox
from pygame.locals import *
from string import letters
from string import whitespace

# Constantes

# Clases
# ---------------------------------------------------------------------
class Avion(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = utils.load_image("images/c3_1_avion.png",True)
        self.rect = self.image.get_rect()
        self.rect.centerx = 1000*utils.RES
        self.rect.centery = 390*utils.RES
        self.vx = -3
        self.sonido = pygame.mixer.Sound("sounds/avioneta.ogg")
        self.sonido.set_volume(0.05)   
    def mover(self):
		if self.rect.centerx < -200*utils.RES:
			self.rect.centerx = 1000*utils.RES
		else:
			self.rect.centerx = self.rect.centerx + self.vx


class Textbox:
	def __init__(self,posx,posy,match,slot,maxlen):
		self.font = pygame.font.Font("fonts/TRSL____.ttf", int(24*utils.RES))
		self.rect = pygame.Rect([0, 0, 220*utils.RES, 22*utils.RES])
		offset = (3, 3)
		self.rect.centerx = posx
		self.rect.centery = posy
		self.rect.left += offset[0]
		self.rect.top  += offset[1]
		self.message = ""
		self.selected = False
		#self.puntos,self.puntos_rect = utils.texto_normal('.......................', posx-20*utils.RES,posy+10*utils.RES)
		self.maxlen = maxlen
		self.sonido = pygame.mixer.Sound("sounds/letra.ogg")
		self.check_img = utils.load_image("images/check.png",True)
		self.cross_img = utils.load_image("images/cross.png",True)
		self.check_rect = self.check_img.get_rect()
		self.check_rect.centerx = posx-30*utils.RES
		self.check_rect.centery = posy-30*utils.RES
		self.match = match
		self.puntos, self.puntos_rect = utils.textotlb(slot, posx-45*utils.RES,posy+5*utils.RES,20,(50,50,50)) 
		
	def display(self,screen):
		screen.blit(self.puntos,self.puntos_rect)
		if len(self.message) != 0:
			#self.message = self.message.encode("iso-8859-1")
			screen.blit(self.font.render(self.message, 1, (0,0,0)), self.rect.topleft)	
   			if self.message == self.match:
				screen.blit(self.check_img,self.check_rect)
			else:
				screen.blit(self.cross_img,self.check_rect)	


# ---------------------------------------------------------------------
def dibujar(screen,cursor_picture, clock):
    salir = 0
    background_image = utils.load_image('images/fondo_3.png',True)
    btn_adelante = objetos.BotonAdelante()
    btn_atras = objetos.BotonAtras()
    clase, clase_rect = utils.texto_clase("Clase 3", 740*utils.RES, 30*utils.RES)
           
    linea1,linea1_rect = utils.texto_normal('Entonces, ahora que ya aprendiste sobre los problemas que ocasiona', 470*utils.RES,80*utils.RES)
    linea2,linea2_rect = utils.texto_normal('la basura y las maneras de evitarlos, tratá de entender las pistas', 450*utils.RES,100*utils.RES)
    linea3,linea3_rect = utils.texto_normal('que nos dio el aviador al principio de la clase.', 383*utils.RES,120*utils.RES)
    
    textboxes = (Textbox(230*utils.RES,280*utils.RES,"e u t i l i z a r ","_ _ _ _ _ _ _ _ _",18),
    Textbox(450*utils.RES,200*utils.RES,"e d u c i r ","_ _  _ _ _ _     ",12),
    Textbox(685*utils.RES,280*utils.RES,"e c i c l a r ","_ _ _ _ _ _ _    ",14))
    
    img1 = utils.load_image('images/c3_13_img6.jpg',True)
    img2 = utils.load_image('images/c3_15b.png',True)
    
    frame = 0
    avion = Avion()
    
    while salir == 0:
    
    	# Pump GTK messages.
		while gtk.events_pending():
			gtk.main_iteration()
			
		for eventos in pygame.event.get():
			if eventos.type == QUIT:
				sys.exit(0)
			elif eventos.type == pygame.VIDEORESIZE:
			   pygame.display.set_mode(eventos.size, pygame.RESIZABLE)	
			elif eventos.type == MOUSEBUTTONDOWN:
				if btn_adelante.rect.collidepoint(pygame.mouse.get_pos()):
					salir = 16
					btn_adelante.sonido.play()	
				elif btn_atras.rect.collidepoint(pygame.mouse.get_pos()):
					salir = 14
					btn_atras.sonido.play()	
				for textbox in textboxes:
					if textbox.rect.collidepoint(pygame.mouse.get_pos()):
						textbox.selected = True
						textbox.sonido.play()
					else:
						textbox.selected = False
			
			elif eventos.type == KEYDOWN:		
				if eventos.key == K_BACKSPACE:
					for textbox in textboxes:
						if textbox.selected == True: 
							textbox.message = textbox.message[0:-2]
				elif eventos.key == K_RETURN:
					break
				else:
					for textbox in textboxes:
						if textbox.selected == True:
							textbox.sonido.play()
							if  len(textbox.message) <= textbox.maxlen:
								textbox.message += eventos.unicode.encode("iso-8859-1")
								textbox.message += " "		
					
		# Realiza la animacion del texto
		avion.mover()
		pygame.draw.rect(screen,(255,255,255),(10*utils.RES,25*utils.RES,780*utils.RES,500*utils.RES))
		screen.blit(avion.image, avion.rect)		
		screen.blit(background_image, (0, 0))
		screen.blit(clase, clase_rect)
		screen.blit(img2, (60*utils.RES, 250*utils.RES))
		screen.blit(img2, (290*utils.RES, 170*utils.RES))
		screen.blit(img2, (520*utils.RES, 250*utils.RES))
		screen.blit(img1, (50*utils.RES, 50*utils.RES))		
		pygame.draw.rect(screen,(150,200,255),(190*utils.RES,60*utils.RES,560*utils.RES,85*utils.RES))
		screen.blit(linea1,linea1_rect.topleft)
		screen.blit(linea2,linea2_rect.topleft)
		screen.blit(linea3,linea3_rect)
		screen.blit(btn_adelante.image, btn_adelante.rect)
		screen.blit(btn_atras.image, btn_atras.rect)
		for textbox in textboxes:
			textbox.display(screen)
		screen.blit(cursor_picture, pygame.mouse.get_pos())
		pygame.display.flip()
		
		# Try to stay at 30 FPS
		clock.tick(30)
                	 
    return salir

# This function is called when the game is run directly from the command line:
# ./TestGame.py 
def main():
	
	pygame.init()
	screen = pygame.display.set_mode((int(800*utils.RES), int(520*utils.RES)), pygame.RESIZABLE) #pygame.display.get_surface() #

	# Fake a Sugar cursor for the pyGame canvas area
	pygame.mouse.set_cursor((8,8),(0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0))
	cursor_picture = pygame.image.load('images/standardcursor.png')
	cursor_picture.convert_alpha() 
	clock = pygame.time.Clock()
	dibujar(screen,cursor_picture,clock)

if __name__ == '__main__':
   main()
